package com.ruoyi.develop.goodsSaledoGongxuUser.service.impl;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
        import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.factory.factoryProduceSaleAll.domain.FactoryProduceSaleAll;
import com.ruoyi.develop.factory.factoryProduceSaleAll.service.IFactoryProduceSaleAllService;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.goodsSaledoGongxu.domain.GoodsSaledoGongxu;
import com.ruoyi.develop.goodsSaledoGongxu.service.IGoodsSaledoGongxuService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.goodsSaledoGongxuUser.mapper.GoodsSaledoGongxuUserMapper;
import com.ruoyi.develop.goodsSaledoGongxuUser.domain.GoodsSaledoGongxuUser;
import com.ruoyi.develop.goodsSaledoGongxuUser.service.IGoodsSaledoGongxuUserService;

/**
 * 订单产品工序分配任务Service业务层处理
 * @author qh
 * @date 2024-11-11
 */
@Service
public class GoodsSaledoGongxuUserServiceImpl implements IGoodsSaledoGongxuUserService {
    @Resource
    private GoodsSaledoGongxuUserMapper goodsSaledoGongxuUserMapper;
    @Autowired
    private IGoodsSaleService goodsSaleService;
    @Autowired
    private IGoodsSaledoGongxuService goodsSaledoGongxuService;
    @Autowired
    private IFactoryProduceSaleAllService factoryProduceSaleAllService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService iSysDeptService;

    /**
     * 查询订单产品工序分配任务
     * @param id 订单产品工序分配任务主键
     * @return 订单产品工序分配任务
     */
    @Override
    public GoodsSaledoGongxuUser selectById(String id) {
        return goodsSaledoGongxuUserMapper.selectById(id);
    }

    /**
     * 查询订单产品工序分配任务列表
     * @param entity 订单产品工序分配任务
     * @return 订单产品工序分配任务
     */
    @Override
    public List<GoodsSaledoGongxuUser> selectList(GoodsSaledoGongxuUser entity) {
        return goodsSaledoGongxuUserMapper.selectList(entity);
    }

    @Override
    public List<GoodsSaledoGongxuUser> listHavedo(GoodsSaledoGongxuUser entity) {
        return goodsSaledoGongxuUserMapper.listHavedo(entity);
    }

    @Override
    public List<GoodsSaledoGongxuUser> listHavedoGroup(GoodsSaledoGongxuUser entity) {
        return goodsSaledoGongxuUserMapper.listHavedoGroup(entity);
    }
    /**
     * 新增订单产品工序分配任务
     * @param entity 订单产品工序分配任务
     * @return 结果
     */
    @Override
    public int insert(GoodsSaledoGongxuUser entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return goodsSaledoGongxuUserMapper.insert(entity);
    }

    @Override
    public int addUserTaskZhuanpai(GoodsSaledoGongxuUser entity) {
        GoodsSaledoGongxuUser goodsSaledoGongxuUser=selectById(entity.getId());
        goodsSaledoGongxuUser.setRemainCount(goodsSaledoGongxuUser.getRemainCount().subtract(entity.getCount()));
        goodsSaledoGongxuUser.setCount(goodsSaledoGongxuUser.getCount().subtract(entity.getCount()));
        update(goodsSaledoGongxuUser);
        String[] userIds=entity.getUserIds().split(",");
        for(String userId:userIds){
            GoodsSaledoGongxuUser newEntity=new GoodsSaledoGongxuUser();
            BeanUtils.copyProperties(goodsSaledoGongxuUser, newEntity, "id");
            newEntity.setFinishCount(BigDecimal.ZERO);
            newEntity.setRemainCount(entity.getCount().divide(BigDecimal.valueOf(userIds.length)));
            newEntity.setCount(entity.getCount());
            newEntity.setPlanEndTime(entity.getPlanEndTime());
            newEntity.setMoney(entity.getMoney());
            newEntity.setAllMoney(entity.getAllMoney().divide(BigDecimal.valueOf(userIds.length)));
            newEntity.setUserId(userId);
            SysUser user1=sysUserService.selectUserById(userId);
            newEntity.setUserName(user1.getUserName());
            insert(newEntity);
        }
        return 1;
    }

    @Override
    public int addUserTask(GoodsSaledoGongxuUser entity) {
        GoodsSaledoGongxu goodsSaledoGongxu=goodsSaledoGongxuService.selectById(entity.getParentId());
        FactoryProduceSaleAll factoryProduceSaleAll=factoryProduceSaleAllService.selectById(goodsSaledoGongxu.getSaleId());
        entity.setProjectId(factoryProduceSaleAll.getProjectId());
        entity.setProjectCode(factoryProduceSaleAll.getProjectNo());
        entity.setCode(goodsSaledoGongxu.getCode());
        entity.setName(goodsSaledoGongxu.getName());
        entity.setGongxuName(goodsSaledoGongxu.getGongxuName());
        entity.setGongxuModel(goodsSaledoGongxu.getGongxuModel());
        entity.setRemark(goodsSaledoGongxu.getRemark());
        entity.setShuoming(goodsSaledoGongxu.getShuoming());
        entity.setSaleId(goodsSaledoGongxu.getSaleId());
        entity.setGoodsId(goodsSaledoGongxu.getGoodsId());
        entity.setGongyiId(goodsSaledoGongxu.getGongyiId());
        entity.setGongxuId(goodsSaledoGongxu.getGongxuId());
        entity.setGongyiCode(goodsSaledoGongxu.getGongyiCode());
        entity.setGoodsCode(factoryProduceSaleAll.getCode());
        entity.setGoodsName(factoryProduceSaleAll.getName());
        entity.setGoodsBrand(factoryProduceSaleAll.getXinghao());
        entity.setGoodsModel(factoryProduceSaleAll.getXinghao());
        //entity.setFinishCount(goodsSale.getCount());
        //entity.setPlanEndTime(goodsSaledoGongxu.getPlanendTime());
        entity.setRemainCount(entity.getCount());

        goodsSaledoGongxu.setFenpeiCount(goodsSaledoGongxu.getFenpeiCount().add(entity.getCount()));//分配数量
        goodsSaledoGongxu.setRemainCount(goodsSaledoGongxu.getCounts().subtract(goodsSaledoGongxu.getFenpeiCount()));//剩余分配数量
        goodsSaledoGongxu.setRemainfinishCount(goodsSaledoGongxu.getFenpeiCount().subtract(goodsSaledoGongxu.getFinishCount()));//剩余完成数量
        if(goodsSaledoGongxu.getRemainCount().compareTo(BigDecimal.ZERO) <= 0){
            goodsSaledoGongxu.setStatus(BusinessConstants.GONGXU_STATUS_2);
        }else{
            goodsSaledoGongxu.setStatus(BusinessConstants.GONGXU_STATUS_1);
        }
        goodsSaledoGongxuService.update(goodsSaledoGongxu);
        return insert(entity);
    }

    //一起干
   /* @Override
    public int addUserTaskPiliang(GoodsSaledoGongxuUser entitys) {
        String[] userIds=entitys.getUserIds().split(",");
        List<GoodsSaledoGongxu> rows = entitys.getColumnsGongxu();
        if (!rows.isEmpty()) {
            if(userIds.length>0){
                String userNames="";
                for(String userId : userIds){
                    SysUser sysUser=sysUserService.selectUserById(userId);
                    userNames=userNames+sysUser.getNickName()+",";
                }
                BigDecimal userIdsCount=new BigDecimal(String.valueOf(userIds.length));
                    for (GoodsSaledoGongxu goodsSaledoGongxu : rows) {
                        // GoodsSaledoGongxu goodsSaledoGongxu = goodsSaledoGongxuService.selectById(entity.getParentId());
                        GoodsSale goodsSale = goodsSaleService.selectById(goodsSaledoGongxu.getSaleId());
                        GoodsSaledoGongxuUser goodsSaledoGongxuUser = new GoodsSaledoGongxuUser();
                        goodsSaledoGongxuUser.setProjectId(goodsSale.getProjectId());
                        goodsSaledoGongxuUser.setProjectName(goodsSale.getProjectName());
                        goodsSaledoGongxuUser.setProjectCode(goodsSale.getProjectNo());
                        goodsSaledoGongxuUser.setCode(goodsSaledoGongxu.getCode());
                        goodsSaledoGongxuUser.setName(goodsSaledoGongxu.getName());
                        goodsSaledoGongxuUser.setRemark(goodsSaledoGongxu.getRemark());
                        goodsSaledoGongxuUser.setShuoming(goodsSaledoGongxu.getShuoming());
                        goodsSaledoGongxuUser.setSaleId(goodsSaledoGongxu.getSaleId());
                        goodsSaledoGongxuUser.setGoodsId(goodsSaledoGongxu.getGoodsId());
                        goodsSaledoGongxuUser.setGongyiId(goodsSaledoGongxu.getGongyiId());
                        goodsSaledoGongxuUser.setGongxuId(goodsSaledoGongxu.getGongxuId());
                        goodsSaledoGongxuUser.setGongyiCode(goodsSaledoGongxu.getGongyiCode());
                        goodsSaledoGongxuUser.setGoodsCode(goodsSale.getGoodsCode());
                        goodsSaledoGongxuUser.setGoodsName(goodsSale.getGoodsName());
                        goodsSaledoGongxuUser.setGoodsBrand(goodsSale.getGoodsBrand());
                        goodsSaledoGongxuUser.setGoodsModel(goodsSale.getGoodModel());
                        //entity.setFinishCount(goodsSale.getCount());
                        //entity.setPlanEndTime(goodsSaledoGongxu.getPlanendTime());

                        goodsSaledoGongxuUser.setCount(goodsSaledoGongxu.getRemainCount());
                        goodsSaledoGongxuUser.setMoney(goodsSaledoGongxu.getMoney());
                        goodsSaledoGongxuUser.setAllMoney(goodsSaledoGongxu.getRemainCount().multiply(goodsSaledoGongxu.getMoney()));
                        goodsSaledoGongxuUser.setRemainCount(goodsSaledoGongxu.getRemainCount());
                        goodsSaledoGongxuUser.setUserId(entitys.getUserIds());
                        goodsSaledoGongxuUser.setUserName(userNames);
                        goodsSaledoGongxuUser.setParentId(goodsSaledoGongxu.getId());

                        goodsSaledoGongxu.setFenpeiCount(goodsSaledoGongxu.getFenpeiCount().add(goodsSaledoGongxu.getRemainCount()));//分配数量
                        goodsSaledoGongxu.setRemainCount(goodsSaledoGongxu.getCounts().subtract(goodsSaledoGongxu.getFenpeiCount()));//剩余分配数量
                        goodsSaledoGongxu.setRemainfinishCount(goodsSaledoGongxu.getFenpeiCount().subtract(goodsSaledoGongxu.getFinishCount()));//剩余完成数量
                        //goodsSaledoGongxu.setStatus(BusinessConstants.GONGXU_STATUS_2);
                        if (goodsSaledoGongxu.getRemainCount().compareTo(BigDecimal.ZERO) <= 0) {
                            goodsSaledoGongxu.setStatus(BusinessConstants.GONGXU_STATUS_2);
                        } else {
                            goodsSaledoGongxu.setStatus(BusinessConstants.GONGXU_STATUS_1);
                        }
                        goodsSaledoGongxuService.update(goodsSaledoGongxu);
                        insert(goodsSaledoGongxuUser);
                    }
                }
        }
        return rows.size();
    }*/

    //按计件批量分活平均分
    @Override
    public int addUserTaskPiliang(GoodsSaledoGongxuUser entitys) {
        String[] userIds=entitys.getUserIds().split(",");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        List<GoodsSaledoGongxu> rows = entitys.getColumnsGongxu();
        if (!rows.isEmpty()) {
            if(userIds.length>0){
                for (GoodsSaledoGongxu goodsSaledoGongxu : rows) {
                    for(String userId : userIds){
                        SysUser sysUser=sysUserService.selectUserById(userId);
                        //GoodsSale goodsSale = goodsSaleService.selectById(goodsSaledoGongxu.getSaleId());
                        FactoryProduceSaleAll factoryProduceSaleAll=factoryProduceSaleAllService.selectById(goodsSaledoGongxu.getSaleId());
                        GoodsSaledoGongxuUser goodsSaledoGongxuUser = new GoodsSaledoGongxuUser();
                        goodsSaledoGongxuUser.setXuhao(goodsSaledoGongxu.getXuhao());
                        goodsSaledoGongxuUser.setIslast(goodsSaledoGongxu.getIslast());
                        goodsSaledoGongxuUser.setPlanStartTime(entitys.getPlanStartTime());
                        if(factoryProduceSaleAll!=null) {
                            factoryProduceSaleAll.setStatus(BusinessConstants.PROD_STATUS_3);
                            factoryProduceSaleAllService.update(factoryProduceSaleAll);
                            goodsSaledoGongxuUser.setProjectId(factoryProduceSaleAll.getProjectId());
                            //goodsSaledoGongxuUser.setProjectName(factoryProduceSaleAll.getProjectName());
                            goodsSaledoGongxuUser.setProjectCode(factoryProduceSaleAll.getProjectNo());
                            goodsSaledoGongxuUser.setGoodsCode(factoryProduceSaleAll.getCode());
                            goodsSaledoGongxuUser.setGoodsName(factoryProduceSaleAll.getName());
                            //goodsSaledoGongxuUser.setGoodsBrand(factoryProduceSaleAll.getGoodsBrand());
                            goodsSaledoGongxuUser.setGoodsModel(factoryProduceSaleAll.getXinghao());
                        }
                        try {
                            Date date=format.parse(entitys.getPaigongTime());
                            goodsSaledoGongxuUser.setPlanStartTime(date);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        goodsSaledoGongxuUser.setCode(goodsSaledoGongxu.getCode());
                        goodsSaledoGongxuUser.setName(goodsSaledoGongxu.getName());
                        goodsSaledoGongxuUser.setGongxuName(goodsSaledoGongxu.getGongxuName());
                        goodsSaledoGongxuUser.setGongxuModel(goodsSaledoGongxu.getGongxuModel());
                        goodsSaledoGongxuUser.setRemark(goodsSaledoGongxu.getRemark());
                        goodsSaledoGongxuUser.setShuoming(goodsSaledoGongxu.getShuoming());
                        goodsSaledoGongxuUser.setSaleId(goodsSaledoGongxu.getSaleId());
                        goodsSaledoGongxuUser.setGoodsId(goodsSaledoGongxu.getGoodsId());
                        goodsSaledoGongxuUser.setGongyiId(goodsSaledoGongxu.getGongyiId());
                        goodsSaledoGongxuUser.setGongxuId(goodsSaledoGongxu.getGongxuId());
                        goodsSaledoGongxuUser.setGongyiCode(goodsSaledoGongxu.getGongyiCode());
                        //entity.setFinishCount(goodsSale.getCount());
                        //entity.setPlanEndTime(goodsSaledoGongxu.getPlanendTime());

                        goodsSaledoGongxuUser.setCount(goodsSaledoGongxu.getRemainCount().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
                        goodsSaledoGongxuUser.setMoney(goodsSaledoGongxu.getMoney());
                        goodsSaledoGongxuUser.setAllMoney(goodsSaledoGongxu.getRemainCount().multiply(goodsSaledoGongxu.getMoney()).divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
                        goodsSaledoGongxuUser.setRemainCount(goodsSaledoGongxu.getRemainCount().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
                        if(sysUser!=null){
                            goodsSaledoGongxuUser.setUserId(sysUser.getUserId());
                            goodsSaledoGongxuUser.setUserName(sysUser.getUserName());
                            if(StringUtils.isNotBlank(sysUser.getDeptId())){
                                SysDept sysDept=iSysDeptService.selectDeptById(sysUser.getDeptId());
                                if(sysDept!=null){
                                    goodsSaledoGongxuUser.setDeptName(sysDept.getDeptName());
                                }
                            }
                        }
                        goodsSaledoGongxuUser.setParentId(goodsSaledoGongxu.getId());
                        goodsSaledoGongxuUser.setType(goodsSaledoGongxu.getType());
                        insert(goodsSaledoGongxuUser);
                    }

                    goodsSaledoGongxu.setFenpeiCount(goodsSaledoGongxu.getFenpeiCount().add(goodsSaledoGongxu.getRemainCount()));//分配数量
                    goodsSaledoGongxu.setRemainCount(goodsSaledoGongxu.getCounts().subtract(goodsSaledoGongxu.getFenpeiCount()));//剩余分配数量
                    goodsSaledoGongxu.setRemainfinishCount(goodsSaledoGongxu.getFenpeiCount().subtract(goodsSaledoGongxu.getFinishCount()));//剩余完成数量
                    //goodsSaledoGongxu.setStatus(BusinessConstants.GONGXU_STATUS_2);
                    if (goodsSaledoGongxu.getRemainCount().compareTo(BigDecimal.ZERO) <= 0) {
                        goodsSaledoGongxu.setStatus(BusinessConstants.GONGXU_STATUS_2);
                    } else {
                        goodsSaledoGongxu.setStatus(BusinessConstants.GONGXU_STATUS_1);
                    }
                    goodsSaledoGongxuService.update(goodsSaledoGongxu);
                }
            }
        }
        return rows.size();
    }

    //按计时批量分活平均分
    @Override
    public int addUserTaskJishiPiliang(GoodsSaledoGongxuUser entitys) {
        String[] userIds=entitys.getUserIds().split(",");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        List<GoodsSaledoGongxu> rows = entitys.getColumnsGongxu();
        if (!rows.isEmpty()) {
            if(userIds.length>0){
                for (GoodsSaledoGongxu goodsSaledoGongxu : rows) {
                    for(String userId : userIds){
                        SysUser sysUser=sysUserService.selectUserById(userId);
                        //GoodsSale goodsSale = goodsSaleService.selectById(goodsSaledoGongxu.getSaleId());
                        FactoryProduceSaleAll factoryProduceSaleAll=factoryProduceSaleAllService.selectById(goodsSaledoGongxu.getSaleId());
                        GoodsSaledoGongxuUser goodsSaledoGongxuUser = new GoodsSaledoGongxuUser();
                        goodsSaledoGongxuUser.setXuhao(goodsSaledoGongxu.getXuhao());
                        goodsSaledoGongxuUser.setIslast(goodsSaledoGongxu.getIslast());
                        if(factoryProduceSaleAll!=null){
                            goodsSaledoGongxuUser.setProjectId(factoryProduceSaleAll.getProjectId());
                            goodsSaledoGongxuUser.setProjectCode(factoryProduceSaleAll.getProjectNo());
                            goodsSaledoGongxuUser.setGoodsCode(factoryProduceSaleAll.getCode());
                            goodsSaledoGongxuUser.setGoodsName(factoryProduceSaleAll.getName());
                            //goodsSaledoGongxuUser.setGoodsBrand(factoryProduceSaleAll.getGoodsBrand());
                            goodsSaledoGongxuUser.setGoodsModel(factoryProduceSaleAll.getXinghao());
                        }
                        try {
                            Date date=format.parse(entitys.getPaigongTime());
                            goodsSaledoGongxuUser.setPlanStartTime(date);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        //goodsSaledoGongxuUser.setProjectName(factoryProduceSaleAll.getProjectName());
                        goodsSaledoGongxuUser.setCode(goodsSaledoGongxu.getCode());
                        goodsSaledoGongxuUser.setName(goodsSaledoGongxu.getName());
                        goodsSaledoGongxuUser.setGongxuName(goodsSaledoGongxu.getGongxuName());
                        goodsSaledoGongxuUser.setGongxuModel(goodsSaledoGongxu.getGongxuModel());
                        goodsSaledoGongxuUser.setRemark(goodsSaledoGongxu.getRemark());
                        goodsSaledoGongxuUser.setShuoming(goodsSaledoGongxu.getShuoming());
                        goodsSaledoGongxuUser.setSaleId(goodsSaledoGongxu.getSaleId());
                        goodsSaledoGongxuUser.setGoodsId(goodsSaledoGongxu.getGoodsId());
                        goodsSaledoGongxuUser.setGongyiId(goodsSaledoGongxu.getGongyiId());
                        goodsSaledoGongxuUser.setGongxuId(goodsSaledoGongxu.getGongxuId());
                        goodsSaledoGongxuUser.setGongyiCode(goodsSaledoGongxu.getGongyiCode());
                        //entity.setFinishCount(goodsSale.getCount());
                        //entity.setPlanEndTime(goodsSaledoGongxu.getPlanendTime());

                        goodsSaledoGongxuUser.setCount(goodsSaledoGongxu.getRemainCount().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
                        goodsSaledoGongxuUser.setMoney(goodsSaledoGongxu.getMoney());
                        goodsSaledoGongxuUser.setRemainCount(goodsSaledoGongxu.getRemainCount().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
                        if(sysUser!=null){
                            goodsSaledoGongxuUser.setUserId(sysUser.getUserId());
                            goodsSaledoGongxuUser.setUserName(sysUser.getUserName());
                            if(StringUtils.isNotBlank(sysUser.getDeptId())){
                                SysDept sysDept=iSysDeptService.selectDeptById(sysUser.getDeptId());
                                if(sysDept!=null){
                                    goodsSaledoGongxuUser.setDeptName(sysDept.getDeptName());
                                }
                            }
                        }
                        goodsSaledoGongxuUser.setParentId(goodsSaledoGongxu.getId());
                        goodsSaledoGongxuUser.setType(goodsSaledoGongxu.getType());
                        goodsSaledoGongxuUser.setHour(goodsSaledoGongxu.getHour().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
                        goodsSaledoGongxuUser.setAllMoney(goodsSaledoGongxu.getHour().multiply(goodsSaledoGongxu.getMoney()).divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
                        insert(goodsSaledoGongxuUser);
                    }

                    goodsSaledoGongxu.setFenpeiCount(goodsSaledoGongxu.getFenpeiCount().add(goodsSaledoGongxu.getRemainCount()));//分配数量
                    goodsSaledoGongxu.setRemainCount(goodsSaledoGongxu.getCounts().subtract(goodsSaledoGongxu.getFenpeiCount()));//剩余分配数量
                    goodsSaledoGongxu.setRemainfinishCount(goodsSaledoGongxu.getFenpeiCount().subtract(goodsSaledoGongxu.getFinishCount()));//剩余完成数量
                    //goodsSaledoGongxu.setStatus(BusinessConstants.GONGXU_STATUS_2);
                    if (goodsSaledoGongxu.getRemainCount().compareTo(BigDecimal.ZERO) <= 0) {
                        goodsSaledoGongxu.setStatus(BusinessConstants.GONGXU_STATUS_2);
                    } else {
                        goodsSaledoGongxu.setStatus(BusinessConstants.GONGXU_STATUS_1);
                    }
                    goodsSaledoGongxuService.update(goodsSaledoGongxu);
                }
            }
        }
        return rows.size();
    }

    //杂活按计件批量分活平均分
    @Override
    public int addUserTaskPiliangZahuo(GoodsSaledoGongxuUser entity) {
        String[] userIds=entity.getUserIds().split(",");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        GoodsSaledoGongxu goodsSaledoGongxu = new GoodsSaledoGongxu();
        goodsSaledoGongxu.setGongxuName(entity.getGongxuName());
        goodsSaledoGongxu.setGongxuModel(entity.getGongxuModel());
        goodsSaledoGongxu.setName(entity.getName());
        goodsSaledoGongxu.setGoodsName(entity.getGongxuName());
        goodsSaledoGongxu.setGoodsModel(entity.getGongxuModel());

        goodsSaledoGongxu.setType(entity.getType());
        goodsSaledoGongxu.setStatus(BusinessConstants.GONGXU_STATUS_2);
        goodsSaledoGongxu.setRemark(entity.getRemark());

        goodsSaledoGongxu.setCounts(entity.getCount().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
        goodsSaledoGongxu.setMoney(entity.getMoney());
        goodsSaledoGongxu.setFenpeiCount(entity.getCount().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
        goodsSaledoGongxu.setRemainfinishCount(entity.getCount().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
        goodsSaledoGongxu.setDeptId(SecurityUtils.getDeptId());
        goodsSaledoGongxu.setDeptName(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
        goodsSaledoGongxu.setUpdateBy(SecurityUtils.getUsername());//派工人
        goodsSaledoGongxu.setUpdateTime(DateUtils.getNowDate());
        goodsSaledoGongxu.setShuoming(entity.getShuoming());
        goodsSaledoGongxuService.insert(goodsSaledoGongxu);

        if(userIds.length>0) {
            for (String userId : userIds) {
                SysUser sysUser = sysUserService.selectUserById(userId);

                GoodsSaledoGongxuUser goodsSaledoGongxuUser = new GoodsSaledoGongxuUser();
                goodsSaledoGongxuUser.setPlanStartTime(entity.getPlanStartTime());
                goodsSaledoGongxuUser.setXuhao(goodsSaledoGongxu.getXuhao());
                goodsSaledoGongxuUser.setIslast(goodsSaledoGongxu.getIslast());
                goodsSaledoGongxuUser.setParentId(goodsSaledoGongxu.getId());
                goodsSaledoGongxuUser.setGongxuName(entity.getGongxuName());
                goodsSaledoGongxuUser.setGongxuModel(entity.getGongxuModel());
                goodsSaledoGongxuUser.setName(entity.getName());
                goodsSaledoGongxuUser.setGoodsName(entity.getGongxuName());
                goodsSaledoGongxuUser.setGoodsModel(entity.getGongxuModel());
                goodsSaledoGongxuUser.setType(entity.getType());
                goodsSaledoGongxuUser.setRemark(entity.getRemark());
                try {
                    Date date=format.parse(entity.getPaigongTime());
                    goodsSaledoGongxuUser.setPlanStartTime(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                goodsSaledoGongxuUser.setCount(entity.getCount().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
                goodsSaledoGongxuUser.setMoney(entity.getMoney());
                goodsSaledoGongxuUser.setAllMoney(entity.getCount().multiply(entity.getMoney()).divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
                goodsSaledoGongxuUser.setRemainCount(entity.getCount().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
                if (sysUser != null) {
                    goodsSaledoGongxuUser.setUserId(sysUser.getUserId());
                    goodsSaledoGongxuUser.setUserName(sysUser.getUserName());
                    if (StringUtils.isNotBlank(sysUser.getDeptId())) {
                        SysDept sysDept = iSysDeptService.selectDeptById(sysUser.getDeptId());
                        if (sysDept != null) {
                            goodsSaledoGongxuUser.setDeptName(sysDept.getDeptName());
                        }
                    }
                }
                goodsSaledoGongxuUser.setShuoming(entity.getShuoming());
                //goodsSaledoGongxuUser.setDeptName(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
                insert(goodsSaledoGongxuUser);
            }
        }
        return 1;
    }

    //杂活按计时批量分活平均分
    @Override
    public int addUserTaskPiliangJishiZahuo(GoodsSaledoGongxuUser entity) {
        String[] userIds=entity.getUserIds().split(",");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        GoodsSaledoGongxu goodsSaledoGongxu = new GoodsSaledoGongxu();
        goodsSaledoGongxu.setGongxuName(entity.getGongxuName());
        goodsSaledoGongxu.setGongxuModel(entity.getGongxuModel());
        goodsSaledoGongxu.setName(entity.getName());
        goodsSaledoGongxu.setGoodsName(entity.getGongxuName());
        goodsSaledoGongxu.setGoodsModel(entity.getGongxuModel());

        goodsSaledoGongxu.setType(entity.getType());
        goodsSaledoGongxu.setStatus(BusinessConstants.GONGXU_STATUS_2);
        goodsSaledoGongxu.setRemark(entity.getRemark());

        goodsSaledoGongxu.setCounts(entity.getCount().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
        goodsSaledoGongxu.setMoney(entity.getMoney());
        goodsSaledoGongxu.setFenpeiCount(entity.getCount().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
        goodsSaledoGongxu.setRemainfinishCount(entity.getCount().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
        goodsSaledoGongxu.setHour(entity.getHour());
        goodsSaledoGongxu.setDeptId(SecurityUtils.getDeptId());
        goodsSaledoGongxu.setDeptName(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
        goodsSaledoGongxu.setUpdateBy(SecurityUtils.getUsername());//派工人
        goodsSaledoGongxu.setUpdateTime(DateUtils.getNowDate());
        goodsSaledoGongxu.setShuoming(entity.getShuoming());
        goodsSaledoGongxuService.insert(goodsSaledoGongxu);

        if(userIds.length>0) {
            for (String userId : userIds) {
                SysUser sysUser = sysUserService.selectUserById(userId);

                GoodsSaledoGongxuUser goodsSaledoGongxuUser = new GoodsSaledoGongxuUser();
                goodsSaledoGongxuUser.setPlanStartTime(entity.getPlanStartTime());
                goodsSaledoGongxuUser.setXuhao(goodsSaledoGongxu.getXuhao());
                goodsSaledoGongxuUser.setIslast(goodsSaledoGongxu.getIslast());
                goodsSaledoGongxuUser.setGongxuName(entity.getGongxuName());
                goodsSaledoGongxuUser.setGongxuModel(entity.getGongxuModel());
                goodsSaledoGongxuUser.setName(entity.getName());
                goodsSaledoGongxuUser.setGoodsName(entity.getGongxuName());
                goodsSaledoGongxuUser.setGoodsModel(entity.getGongxuModel());
                goodsSaledoGongxuUser.setType(entity.getType());
                goodsSaledoGongxuUser.setRemark(entity.getRemark());
                goodsSaledoGongxuUser.setHour(entity.getHour());
                goodsSaledoGongxuUser.setCount(entity.getRemainCount().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
                goodsSaledoGongxuUser.setMoney(entity.getMoney());
                goodsSaledoGongxuUser.setRemainCount(entity.getRemainCount().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));

                try {
                    Date date=format.parse(entity.getPaigongTime());
                    goodsSaledoGongxuUser.setPlanStartTime(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                goodsSaledoGongxuUser.setHour(entity.getHour().divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
                goodsSaledoGongxuUser.setAllMoney(entity.getHour().multiply(goodsSaledoGongxu.getMoney()).divide(BigDecimal.valueOf(userIds.length), 3, RoundingMode.HALF_UP));
                if (sysUser != null) {
                    goodsSaledoGongxuUser.setUserId(sysUser.getUserId());
                    goodsSaledoGongxuUser.setUserName(sysUser.getUserName());
                    if (StringUtils.isNotBlank(sysUser.getDeptId())) {
                        SysDept sysDept = iSysDeptService.selectDeptById(sysUser.getDeptId());
                        if (sysDept != null) {
                            goodsSaledoGongxuUser.setDeptName(sysDept.getDeptName());
                        }
                    }
                }
                goodsSaledoGongxuUser.setShuoming(entity.getShuoming());
                insert(goodsSaledoGongxuUser);
            }
        }
        return 1;
    }
    /**
     * 修改订单产品工序分配任务
     * @param entity 订单产品工序分配任务
     * @return 结果
     */
    @Override
    public int update(GoodsSaledoGongxuUser entity) {
                entity.setUpdateBy(SecurityUtils.getUsername());
                entity.setUpdateTime(DateUtils.getNowDate());
        return goodsSaledoGongxuUserMapper.update(entity);
    }

    @Override
    public int updateCounts(GoodsSaledoGongxuUser entity) {
        entity.setAllMoney(entity.getMoney().multiply(entity.getCount()));
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        entity.setRemainCount(entity.getCount().subtract(entity.getFinishCount()));//剩余完成数量
        GoodsSaledoGongxuUser goodsSaledoGongxuUser=goodsSaledoGongxuUserMapper.selectById(entity.getId());
        if(StringUtils.isNotBlank(entity.getParentId())){
            GoodsSaledoGongxu goodsSaledoGongxu=goodsSaledoGongxuService.selectById(entity.getParentId());
            if(goodsSaledoGongxu!=null){
                goodsSaledoGongxu.setFenpeiCount(goodsSaledoGongxu.getFenpeiCount().subtract(goodsSaledoGongxuUser.getCount()).add(entity.getCount()));
                goodsSaledoGongxu.setRemainCount(goodsSaledoGongxu.getCounts().subtract(goodsSaledoGongxu.getFenpeiCount()));//剩余分配数量
                goodsSaledoGongxuService.update(goodsSaledoGongxu);

            }
        }
        return goodsSaledoGongxuUserMapper.update(entity);
    }

    /**
     * 批量删除订单产品工序分配任务
     * @param ids 需要删除的订单产品工序分配任务主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return goodsSaledoGongxuUserMapper.deleteByIds(ids);
    }

    /**
     * 删除订单产品工序分配任务信息
     * @param id 订单产品工序分配任务主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return goodsSaledoGongxuUserMapper.deleteById(id);
    }
    @Override
    public int deleteByIdsUpdate(String id) {
        GoodsSaledoGongxuUser entity=goodsSaledoGongxuUserMapper.selectById(id);
        GoodsSaledoGongxu goodsSaledoGongxu=goodsSaledoGongxuService.selectById(entity.getParentId());
        goodsSaledoGongxu.setFenpeiCount(goodsSaledoGongxu.getFenpeiCount().subtract(entity.getCount()));
        //goodsSaledoGongxu.setRemainfinishCount(goodsSaledoGongxu.getRemainfinishCount().add(entity.getCount()));
        goodsSaledoGongxuService.update(goodsSaledoGongxu);
        return goodsSaledoGongxuUserMapper.deleteById(id);
    }
    @Override
    public int editAllTuzhihao(GoodsSaledoGongxuUser entity) {
        for(String id:entity.getIds()){
            GoodsSaledoGongxuUser lnGongyigongxuUser = goodsSaledoGongxuUserMapper.selectById(id);
            lnGongyigongxuUser.setShuoming(entity.getShuoming());
            lnGongyigongxuUser.setName(entity.getName());
            lnGongyigongxuUser.setGongxuName(entity.getGongxuName());
            lnGongyigongxuUser.setGongxuModel(entity.getGongxuModel());
            lnGongyigongxuUser.setUpdateBy(SecurityUtils.getUsername());
            lnGongyigongxuUser.setUpdateTime(DateUtils.getNowDate());
            goodsSaledoGongxuUserMapper.update(lnGongyigongxuUser);
        }
        return entity.getIds().length;
    }
    @Override
    public int updateStatusByParentId(String status,String parentId) {
        return goodsSaledoGongxuUserMapper.updateStatusByParentId(status,parentId);
    }
    @Override
    public void updateAllNotLast(String gongyiId) {
        goodsSaledoGongxuUserMapper.updateAllNotLast(gongyiId);
    }
    @Override
    public void updateLastById(String id) {
        goodsSaledoGongxuUserMapper.updateLastById( id);
    }
}
